/*
 * @Author: stark
 * @Date: 2022-06-28 11:24:47
 */

$(function () {
    $.cookie.json = true;
    let id = $.getUrlParam('id');
    let userInfo = $.cookie('userInfo') ? $.cookie('userInfo') : null;

    // 如果有地址那么就是渲染已有地址
    if (id) {
        let data = {
            userId: userInfo.id,
            addressId: id
        }
        let html = '';
        let html2 = '';
        let html3 = '';
        $.get(baseUrl + '/address/detail', data, res => {
            let result = res.data;
            $.post(baseUrl + '/system/province', { parentId: 1 }, res => {
                let province = res.data;
                province.forEach(item => {
                    html += ` <option value="${item.id}" ${item.id == result.province ? 'selected' : ''}>${item.name}</option>`
                })
                $("#province").html(html);

                $.post(baseUrl + '/system/province', { parentId: $("#province").val() }, res => {
                    let city = res.data;
                    city.forEach(item => {
                        html2 += `<option value="${item.id}" ${item.id == result.city ? 'selected' : ''}>${item.name}</option>`
                    })
                    $("#city").html(html2);

                    $.post(baseUrl + '/system/province', { parentId: $("#city").val() }, res => {
                        let area = res.data;
                        area.forEach(item => {
                            html3 += `<option value="${item.id}" ${item.id == result.area ? 'selected' : ''}>${item.name}</option>`
                        })
                        $("#area").html(html3);
                    });
                });
            });

            $(".uname").val(result.consignee);
            $(".mobile").val(result.mobile);
            $(".address").val(result.address);


        })
        fn();
    } else {
        fn();
    }





    function fn() {
        let html = '<option value="">请选择省</option>';
        $.post(baseUrl + '/system/province', { parentId: 1 }, res => {
            let province = res.data;
            province.forEach(item => {
                html += ` <option value="${item.id}">${item.name}</option>`
            })
            $("#province").html(html);
        });

        $("#province").on('change', function () {
            let html = '<option value="">请选择市</option>';
            $.post(baseUrl + '/system/province', { parentId: $(this).val() }, res => {
                let city = res.data;
                city.forEach(item => {
                    html += `<option value="${item.id}">${item.name}</option>`
                })
                $("#city").html(html);
            });
        })

        $("#city").on('change', function () {
            let html = '<option value="">请选择区</option>';
            $.post(baseUrl + '/system/province', { parentId: $(this).val() }, res => {
                let area = res.data;
                area.forEach(item => {
                    html += `<option value="${item.id}">${item.name}</option>`
                })
                $("#area").html(html);
            });
        })

    }


    // 页面数据提交
    $(".submit").on('click', function () {
        // 提交页面窗口关闭
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
        // 关闭后刷新
        parent.location.reload();

        if (id) {
            let data = getInfo();
            $.post(baseUrl + '/address/change', getInfo2(), response => {
                console.log(response);
            })
        } else {
            $.post(baseUrl + '/address/add', getInfo(), response => {
                console.log(response);
            });
        }
    })

    $(".cancel").on('click', function () {
        // 提交页面窗口关闭
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
        // parent.location.reload();
    })


    function getInfo() {
        let uname = $(".uname").val();
        let mobile = $(".mobile").val();
        let province = $("#province").val();
        let city = $("#city").val();
        let area = $("#area").val();
        let address = $(".address").val();
        let data = {

            // 收货人
            consignee: uname,
            // 手机号
            mobile,
            // 省
            province,
            // 市
            city,
            // 区
            area,
            // 地址
            address,
            // 默认0
            isDefault: 0,
            // 用户id
            userId: userInfo.id
        }
        return data
    }


    function getInfo2() {
        let uname = $(".uname").val();
        let mobile = $(".mobile").val();
        let province = $("#province").val();
        let city = $("#city").val();
        let area = $("#area").val();
        let address = $(".address").val();
        let addressId = id;
        let data = {
            addressId,
            // 收货人
            consignee: uname,
            // 手机号
            mobile,
            // 省
            province,
            // 市
            city,
            // 区
            area,
            // 地址
            address,
            // 默认0
            isDefault: 0,
            // 用户id
            userId: userInfo.id
        }
        return data

    }

})




